package top.lcpsky.flashcards.service;

import org.springframework.stereotype.*;

import top.lcpsky.flashcards.entity.Folder;
import top.lcpsky.flashcards.entity.FolderView;
import top.lcpsky.flashcards.mapper.FolderMapper;
import top.lcpsky.flashcards.mapper.NodeMapper;
import top.lcpsky.flashcards.util.FolderUtil;
import top.lcpsky.flashcards.util.ServerTimeUtil;

import javax.annotation.*;
import javax.servlet.http.*;

import com.google.gson.Gson;

import java.util.*;

@Service
public class FolderViewService {
	@Resource
	private FolderUtil fu;
	@Resource
	private FolderMapper fm;
	@Resource
	private NodeMapper flm;
	@Resource
	private Gson gson;

	public String getFolderViewToJson(final String fid, final HttpSession session, final HttpServletRequest request) {
		Folder vf = this.fm.queryById(fid);
		// 检查访问文件夹视图请求是否合法
		final FolderView fv = new FolderView();
		fv.setFolder(vf);
		fv.setParentList(this.fu.getParentList(fid));
		List<Folder> fs = new LinkedList<>();
		for (Folder f : this.fm.queryByParentId(fid)) {
			fs.add(f);
		}
		fv.setFolderList(fs);
		fv.setFileList(this.flm.queryByParentFolderId(fid));
		fv.setAccount((String)session.getAttribute("userName"));
		final List<String> authList = new ArrayList<String>();
		authList.add("U");
		authList.add("C");
		authList.add("D");
		authList.add("R");
		authList.add("L");
		authList.add("M");
		fv.setAuthList(authList);
		fv.setPublishTime(ServerTimeUtil.accurateToMinute());
		return gson.toJson(fv);
	}

}
